package com.niit.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.*;
import org.springframework.stereotype.Service;

@Service
public class RedisService {

    // 注入RedisTemplate
    @Autowired
    private RedisTemplate redisTemplate;

    // 注入ListOperations
    @Autowired
    private ListOperations<String,Object> listOperations;

    //获取该key所对应的列表的长度
    public int llen(String key){
        Long keyList = listOperations.size(key);
        if (keyList != null) return keyList.intValue();
        return 0;
    }

   // 取出列表中的最后一个元素
    public String rpop(String key){
        return (String) listOperations.leftPop(key);
    }
    // 添加列表元素
    public void leftPush(String k,Object v){
        listOperations.leftPush(k,v);
    }

    // 判断缓存中是否有对应的value
    public boolean exists(final String key) {
        return redisTemplate.hasKey(key);
    }
}
